<?php
namespace zhtfrontend\controllers;

use Yii;
use common\components\helpers\Fun;
use zhtfrontend\models\datas\CheckCount;
use yii\data\Pagination;
use yii\web\BadRequestHttpException;
use yii\web\Response;
use zhtfrontend\models\datas\MechanicSettlement;
use zhtfrontend\models\datas\MechanicSettlementRelation;
use zhtfrontend\models\datas\MechanicSettlementSubsidy;
use zhtfrontend\models\datas\ReportRecord;
use yii\widgets\ActiveForm;
use zhtfrontend\models\datas\Admin;

class MechanichourController extends \zhtfrontend\components\bases\BaseController
{
    public function actionIndex()
    {
        $get = \Yii::$app->request->get();
        $model = MechanicSettlement::find()->where(['mechanic_id'=>\Yii::$app->user->identity->mechanic_id]);
        if(!empty($get['id'])){
            $model->andWhere(['id'=>$get['id']]);
        }
        $pages = new Pagination(['totalCount' => $model->count(), 'pageSize' => 14, 'params' => $_GET]);
        $data = $model->offset($pages->offset)->limit($pages->limit)->all();
        $data = Fun::asArray($data,['pay_status_name','mechanic_name','process','mechanicoperate']);
        return $this->render('index',['data'=>$data,'months'=>[]]);
    }


    /**
     *  核实数据
     */
    public function actionVerify(){
        $this->layout='@app/views/layouts/iframe.php';
        $id = \Yii::$app->request->get('id');
        $modelMechanic = MechanicSettlement::findOne($id);
        $model = new ReportRecord();
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'w0' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(\Yii::$app->request->post())&& $model->validate()) {
                $model->module_id = $id;
                $model->sender_id = \Yii::$app->user->id;
                    $stage = 12;
                if($model->status == 1){
                    $row = Admin::find()->where(['role'=>'finance','status'=>1])->one();
                    $sendee_id = $row->id;
                }else{
                    $row = ReportRecord::find()->where(['type'=>2,'stage'=>10,'module_id'=>$id])->orderBy('id DESC')->one();
                    $sendee_id = $row->sender_id;
                }
                $model->sendee_id = $sendee_id;
                $model->stage = $stage;
                $model->type = 2;
                $model->workerremark = $model->remark;
                $model->operator_type = 'square';
                $model->save(false);

            $modelMechanic->status = $stage;
            $modelMechanic->save(false);
            return $this->redirectMessage(\Yii::t('zhtfrontend', '设置成功'), Fun::url(['mechanichour/index']), true);
        }
        return $this->render('verify',['model'=>$model]);
    }

    /**
     * Displays a single Settlement model.
     */
    public function actionView()
    {
        $this->layout='@app/views/layouts/iframe.php';
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'), 404);
        }
        $id = \Yii::$app->request->get('id');
        $model = MechanicSettlement::findOne($id);

        $reportList = ReportRecord::find()->where(['module_id'=>$id,'type'=>2,'is_visual'=>1])->orderBy('id DESC')->all();
        $data = Fun::asArray($reportList,['sender_name','sendee_name','remark']);
        $Subsidy = MechanicSettlementSubsidy::find()->where(['mechanic_settlement_id'=>$id])->all();


        $stage = [10=>'教务生成结算报表',11=>'教服审核',12=>'机构负责人审核',13=>'财务审核',14=>'财务已付款'];

        return $this->render('view', ['model'=>$model,'reportList'=>$data,'subsidy'=>$Subsidy,'stage'=>$stage]);
    }

    public function actionClassfire(){
        $this->layout='@app/views/layouts/iframe.php';
        $id = \Yii::$app->request->get('id');
        if ($id  == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'), 404);
        }
        $ids = MechanicSettlementRelation::find()->select('checkcount_id')->where(['mechanic_settlement_id'=>$id])->column();
        $checkList = CheckCount::find()->where(['id'=>$ids])->all();
        $data = Fun::asArray($checkList,['teacher_name','student','course','class_type']);
        return $this->render('classfire', ['data' => $data]);
    }
}
